package com.maple.common.widget.spinner

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.LinearLayout
import android.widget.TextView
import com.maple.common.R
import com.maple.model.data.KeyValData
import com.maple.utils.ext.layoutInflater

class KeyValSpinnerAdapter(val context: Context): BaseAdapter(){

    private val list: MutableList<KeyValData> = mutableListOf()

    fun setData(l: List<KeyValData>?) {
        if(!l.isNullOrEmpty()) {
            list.addAll(l)
            this.notifyDataSetChanged()
        }
    }


    override fun getCount(): Int = list.size

    override fun getItem(position: Int): Any = list[position]

    override fun getItemId(position: Int): Long = position.toLong()

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var view = convertView
        val holder: ViewHolder
        if (convertView == null) {
            view = context.layoutInflater.inflate(R.layout.item_spinner,null)
            holder = ViewHolder()
            holder.llItem = view.findViewById(R.id.ll_item)
            holder.tvText = view.findViewById(R.id.tv_text)
            view.tag = holder
        } else {
            holder = (view?.tag) as ViewHolder
        }
        val item = list[position]
        holder.tvText?.text = "${item.k}"
        return view!!
    }

    inner class ViewHolder {
        var llItem: LinearLayout? = null
        var tvText: TextView? = null
    }
}